package com.online.usermanagesystem.service.impl;

import com.online.usermanagesystem.dao.EmployeeMapper;
import com.online.usermanagesystem.entity.Employee;
import com.online.usermanagesystem.service.EmployeeManageService;
import com.online.usermanagesystem.utils.ResultUtil;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 雇员管理的处理类
 *
 * @create 2018-04-24 10:35
 */
@Component
public class EmployeeManageServiceImpl implements EmployeeManageService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Object query(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Employee employee;
        try {
            employee = employeeMapper.query(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == employee) {
            return ResultUtil.NOT_FOUND;
        }

        return employee;
    }

    @Override
    public Object add(Employee employee) {
        if ((null == employee) || (null == employee.getCompany_id()) || (null == employee.getName())) {
            return ResultUtil.BAD_REQUEST;
        }

        employee.setHire_status(0);

        Integer count;
        try {
            count = employeeMapper.countByName(employee.getCompany_id(), employee.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.CONFLICT;
        }

        if (null == employee.getAccount_id()) {
            employee.setAccount_id(0);
        }

        if (null == employee.getPrivileges()) {
            employee.setPrivileges("");
        }

        try {
            employeeMapper.insert(employee);
        } catch (DuplicateKeyException e) {
            return ResultUtil.CONFLICT;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.returnKV("id", employee.getId());
    }

    @Override
    public Object del(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer count;
        try {
            count = employeeMapper.countById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.NOT_FOUND;
        }

        try {
            employeeMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object modify(Employee employee) {
        if (null == employee) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer id = employee.getId();
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        //不允许修改的字段
        employee.setCompany_id(null);

        Integer count;
        try {
            count = employeeMapper.countById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.NOT_FOUND;
        }

        try {
            employeeMapper.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object listByAccount(Integer account) {
        if (null == account) {
            return ResultUtil.BAD_REQUEST;
        }

        try {
            return employeeMapper.queryByAccount(account);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }
    }

    @Override
    public Object listByCompany(Integer company, String keyword, Integer index, Integer count) {
        if (null == index) {
            index = 0;
        }

        if (null == count) {
            count = 64;
        }

        List<Employee> list;
        try {
            if (null == keyword) {
                list = employeeMapper.queryByCompany(company, index, count);
            } else {
                list = employeeMapper.searchByCompany(company, keyword.concat("%"), index, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return list;
    }
}
